package cn.edu.scau.cmi.linhongxin.client;

import javafx.scene.input.*;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import java.util.List;
import cn.edu.scau.cmi.linhongxin.singleton.Chairman;
import cn.edu.scau.cmi.linhongxin.singleton.Marshal;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class SingletonClient extends Application{
	@FXML
	private AnchorPane AnchorPane;
	@FXML
	private Button createsingle;
	@FXML
	private Button createmultiton;
	@FXML
	private TextField createinput1;
	@FXML
	private TextField createinput2;
	@FXML 
	private TextArea createoutput;
		
	public void listenButton() {
		createsingle.setOnMouseClicked(new EventHandler<MouseEvent>() {
			
			@Override
			public void handle(MouseEvent arg) {	
			//	createoutput.clear();
				Chairman chairman = Chairman.getInstance(createinput1.getText());
				createoutput.appendText(chairman.getName()+chairman+'\n');
				
			}
		});
		createmultiton.setOnMouseClicked(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent arg) {
				createoutput.clear();
				String name=createinput2.getText();
				boolean notFull=Marshal.insert(name);
				StringBuilder sb=new StringBuilder();
				List<Marshal> marshalsList=Marshal.getInstances();
				for(Marshal s:marshalsList) {
					sb.append(s.toString());
				}
				createoutput.setText(sb.toString());
				if(!notFull) createoutput.appendText("不能再创建更多元帅了"+'\n');
			}
		});
		
	}
	@FXML
	public  void initialize() {	
		listenButton();
	}
	private Stage PrimaryStage;
	private AnchorPane rootLayout;
	@Override
	public void start(Stage primaryStage) {
		try {
			this.PrimaryStage = primaryStage;
			this.PrimaryStage.setTitle("单例模式和多例模式");			
			initRootLayout();
					
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void initRootLayout() {
		try {
			FXMLLoader loader = new FXMLLoader();
			loader.setLocation(SingletonClient.class.getResource("/cn/edu/scau/cmi/linhongxin/singleton/Singleton.fxml"));
			rootLayout = (AnchorPane) loader.load();			
			Scene scene = new Scene(rootLayout,1044,677);
			PrimaryStage.setScene(scene);
			PrimaryStage.show();
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		launch(args);
	}
	
}
